package nc.tb.cube.model;

import nc.tb.cube.slice.ISliceRule;

public class CubeSliceKey {
	private String cubePK = null;
	private ISliceRule aRule = null;
	private String strKey = null;
	public CubeSliceKey(String pk,ISliceRule rule){
		cubePK = pk;
		aRule = rule;
	}
	@Override
	public int hashCode() {
		final int PRIME = 31;
		int result = 1;
		result = PRIME * result + ((aRule == null) ? 0 : aRule.hashCode());
		result = PRIME * result + ((cubePK == null) ? 0 : cubePK.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if(obj == null)
			return false;
		if (this == obj)
			return true;
		/*if (!super.equals(obj))
			return false;*/
		if (getClass() != obj.getClass())
			return false;
		CubeSliceKey ano = (CubeSliceKey)obj;
		
		if(!cubePK.equals(ano.getCubePK()))
			return false;
		if(aRule.getSliceRuleType() != ano.getARule().getSliceRuleType())
			return false;
		return aRule.equals(ano.getARule());
//		return aRule.getQueryCondition().equals(ano.getARule().getQueryCondition());
		/*final CubeSliceKey other = (CubeSliceKey) obj;
		if (aRule == null) {
			if (other.aRule != null)
				return false;
		} else if (!aRule.equals(other.aRule))
			return false;
		if (cubePK == null) {
			if (other.cubePK != null)
				return false;
		} else if (!cubePK.equals(other.cubePK))
			return false;
		return true;*/
	}
	public ISliceRule getARule() {
		return aRule;
	}
	public String getCubePK() {
		return cubePK;
	}
	public String getPrimaryKey(){
		if(strKey == null){
			strKey = cubePK;
			int nType = aRule.getSliceRuleType();
			switch(nType){
				case ISliceRule.SR_TYPE_META:
					strKey += nType;
					break;
				case ISliceRule.SR_TYPE_PLAN:
					strKey += nType;
					strKey += (aRule.getPkPlans() != null && aRule.getPkPlans().length == 1)?aRule.getPkPlans()[0]:aRule.getQueryCondition();
					break;
				case ISliceRule.SR_TYPE_QUERY:
					strKey += nType;
					strKey += aRule.getQueryCondition();
					break;
			}
		}
		return strKey;
	}
}
